package top.humbleyuan.linkedlist;

import top.humbleyuan.datastructure.linkedlist.Node;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 22:13
 * @Des 复制带随机指针的节点
 */
public class LeetCode_138 {
    public static void main(String[] args) {
        /**
         * 数据准备
         */
//        Node head = new Node(1);
//        int[] vals = {};
//        head = Node.formLink(head,vals);
        Node head = new Node(1,null,null);
        head.random = head;

        Node newNode = new Node(head.val,null,null);
        Node oldTemp = head;

        Node root = new Node(0,newNode,null);

        //老Node，新的深拷贝的Node
        Map<Node,Node> cloneNodeMap = new HashMap<>();

        //newNode先要放进去
        cloneNodeMap.put(oldTemp,newNode);

        while (oldTemp != null) {
            newNode.next = getCloneNode(oldTemp.next,cloneNodeMap);
            newNode.random = getCloneNode(oldTemp.random,cloneNodeMap);

            oldTemp = oldTemp.next;
            newNode = newNode.next;
        }

        System.out.println(11);
    }

    public static Node getCloneNode(Node o,Map<Node,Node> map) {
        if(o == null)
            return null;
        if( !map.containsKey(o)) {
            map.put(o,new Node(o.val,null,null));
        }

        return map.get(o);
    }

}
